<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/4.0.0/css/bootstrap.min.css"
          integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">

    <style>
        .img-container {
            overflow: auto;
            border-right: 2px solid #eeeeee;
        }

        .img-ul {
            padding: 10px 0px;
            margin: 0;
            list-style-type: none;
        }

        .img-li {
            margin: 10px 0;
        }

        .active {
            box-shadow: 0px 0px 10px 5px #0a6ebd;
        }

        .select-container{
            padding-top: 12px;
            height: 60px;
            width: 50%;
        }

        .title {
            margin-top:15px;
            font-size: 20px;
            color: #595656;
        }

        .subtitle{
            color: #595656;
            font-weight: 800;
            font-size: 15px;
            margin-top: 10px;

        }

        .options{
            padding-left: 0px;
            font-weight: 400;
            font-size: 14px;
            color: #3d3d3d;
            margin-top: 10px;
        }

        .footer {
            height: 10%;
            border-top: 2px solid #eeeeee;
        }

        .footer-btn{
            margin-left: 30px;
        }

        .loading {
            text-align: center;
            height: 30px;
            color: #555555;
            display: none;
        }

    </style>
    <title>图片浏览</title>
</head>
<body class="container-fluid">

<div class="row" style="height: 100%">
    <div class="col-md-1 img-container" id="img-container">
        <ul id="ul" class="img-ul">
        </ul>
        <div class="loading">
            加载中...
        </div>
    </div>
    <div class="col-md-11">
        <div class="row" style="height: 90%;">
            <div class="col-md-4">
                <div class="select-container  align-items-center">
                    <select class="form-control" onchange="selectOnchange()" id="edit-select">
                        <option value="0">全部</option>
                        <option value="1">未处理</option>
                        <option value="2">已处理</option>
                    </select>
                </div>
                <img id="img-preview" src="" width="100%">
            </div>

            <div class="col-md-8" id="conclusion" style="overflow: auto;overflow-y: scroll">
                <%--类别--%>
            </div>
        </div>
        <div class="row footer justify-content-md-center align-items-center">
            <button type="button" class="btn btn-info col col-lg-2 footer-btn" onclick="prevImage()">上一张</button>
            <button type="button" class="btn btn-info col col-lg-2 footer-btn" onclick="submitAction()">
                保存结论
            </button>
            <button type="button" class="btn btn-info col col-lg-2 footer-btn" onclick="unavailableAction()">
                无效图片
            </button>
            <button type="button" class="btn btn-info col col-lg-2 footer-btn" onclick="nextImage()">下一张</button>
        </div>
    </div>
</div>
</body>
</html>

<script src="${pageContext.request.contextPath}/js/resource/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/popper.js/1.12.9/umd/popper.min.js"
        integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
        crossorigin="anonymous"></script>
<script src="https://cdn.bootcss.com/bootstrap/4.0.0/js/bootstrap.min.js"
        integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
        crossorigin="anonymous"></script>
<script src="${pageContext.request.contextPath}/assets/js/plugins/sweetalert2.min.js"></script>
<script src="${pageContext.request.contextPath}/js/zcommon.js"></script>

<script>

    var lastId = null;
    var requesting = false;

    //筛选条件处理状态
    function selectOnchange() {
        $('#ul').empty();
        lastId = null;
        fetchImages();
    }


    fetchImages();
    //请求图片
    function fetchImages() {

        if (requesting == true){
            return;
        }

        requesting = true;

        var data = {
            'lastId': lastId,
            'flag': $("#edit-select").val()
        }

        $.ajax({
            url: baseUrl.base + 'imageBrower/list',
            method: 'post',
            data: data,
            success: function (res) {

                $('.loading').hide();

                var code = res.code;
                if (code == 0) {
                    var results = res.results;

                    if (results.length == 0) {
                        //已经加载完
                        lastId = -1;
                        return;
                    }

                    var html = '';

                    for (var idx in results) {
                        var result = results[idx];

                        var treatment_img = result.treatment_img;
                        var treatment_imgs = treatment_img.split(";")

                        for (var i in treatment_imgs) {
                            html += "<li class='img-li' onclick='clickThumbImg(this)' data-src= '" + treatment_imgs[i] + "'><img width=100% src='" + treatment_imgs[i] + '?x-oss-process=image/resize,w_70' + "'/></li>";
                        }

                        lastId = result.treatment_id;
                    }

                    $("#ul").append(html);

                    //    模拟点击第一张图片
                    if ($('.img-li.active').length == 0) {
                        $('.img-li:first').trigger('click');
                    }
                }
            },
            complete:function(){
                requesting = false;
            }
        })
    }

    fetchConclusion();

    //请求病症
    function fetchConclusion() {
        $.ajax({
            url: baseUrl.base + 'imageBrower/tongueConclusionList',
            method: 'post',
            success: function (res) {
                var code = res.code;
                if (code == 0) {
                    var results = res.results;

                    var html = '';

                    for (var i = 0; i < results.length; i++) {
                        var title = results[i].title;
                        var type = results[i].type;
                        var list = results[i].list;

                        html += ' <div class="title row"><div class="col-md-2 text-right">' + title + '</div></div>';

                        for (var j = 0; j < list.length; j++) {
                            var subTitle = list[j].title;
                            var subList = list[j].list;

                            html += '<div class="row">';
                            html += '<div class="col-md-2 subtitle text-right">'+subTitle+'</div>';

                            html += '<div class="col-md-10 options">';

                            for (var k = 0; k < subList.length; k++) {

                                var itemTitle = subList[k].content;
                                var itemId = subList[k].id;
                                var groupId = subList[k].type_flag;
                                var inputType = 'radio';
                                if (type == 3) {
                                    inputType = 'checkbox';
                                }

                                html += '<div class="form-check form-check-inline">' +
                                    '<input class="form-check-input" type="' + inputType + '" name="id' + groupId + '" id="id' + itemId + '" value="' + itemId + '">' +
                                    '<label class="form-check-label" for="id' + itemId + '">' + itemTitle + '</label></div>';

                            }

                            html += '</div></div>';
                        }
                    }

                    $('#conclusion').append(html);

                }
            }
        })
    }

    //点击缩略图
    function clickThumbImg(ev) {
        $(ev).addClass('active').siblings().removeClass('active');
        var src = $(ev).attr('data-src');
        $('#img-preview').attr('src', src);

        uncheckInput();

        scrollToVisiable();

        selectAction();
    }

    //滚动到可视区域
    function scrollToVisiable() {
        var container = $('#img-container');
        var scrollTo = $('.img-li.active');

        container.stop();
        container.animate({
            scrollTop: scrollTo.offset().top - container.offset().top + container.scrollTop() - 100
        }, 250);
    }

    //下一张
    function nextImage() {
        var next = $('.img-li.active').next();
        if (next.length == 0){
            return false;
        }

        next.trigger('click');
        return true;
    }

    //上一张
    function prevImage() {
        var prev = $('.img-li.active').prev();
        if (prev.length == 0){
            return false;
        }

        prev.trigger('click');
        return true;
    }

    //点击无效图片
    function unavailableAction() {
        swal({
            title: '确定是无效图片吗?',
            text: "如果设置为无效图片，将无法恢复",
            type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#17a2b8',
            cancelButtonColor: '#d33',
            confirmButtonText: '确定',
            cancelButtonText: '取消'
        }).then(function (value) {
            if (value.value){

                var imageurl = $('#img-preview').attr('src');


                $.ajax({
                    url: baseUrl.base + 'imageBrower/deleteSelet',
                    method: 'post',
                    data: {'imageurl':imageurl},
                    success: function (res) {
                        var code = res.code;
                        if (code == 0) {
                            //进入下一张
                            if(nextImage() == true){
                                $('.img-li.active').prev().remove();
                            }else {
                                $('.img-li.active').remove();
                            }

                        }
                    }
                })
            }
        })
    }

    //点击确定
    function submitAction() {

        var imageurl = $('#img-preview').attr('src');
        var selectContent = ',';

        $('input:radio:checked').each(function () {
            selectContent = selectContent + this.value + ',';
        });

        $('input:checkbox:checked').each(function () {
            selectContent = selectContent + this.value + ',';
        });

        var data = {
            'imageurl': imageurl,
            'selectContent': selectContent,
        }

        $.ajax({
            url: baseUrl.base + 'imageBrower/saveSelet',
            method: 'post',
            data: data,
            success: function (res) {
                var code = res.code;
                if (code == 0) {
                    //进入下一张
                    nextImage();
                }
            }
        })
    }

    //查询图片
    function selectAction() {
        var imageurl = $('#img-preview').attr('src');
        if (imageurl == undefined || imageurl == null) {
            return;
        }

        $.ajax({
            url: baseUrl.base + 'imageBrower/select',
            method: 'post',
            data: {'imageurl': imageurl},
            success: function (res) {
                var code = res.code;
                if (code == 0) {
                    var results = res.results;
                    if (results.length > 0) {
                        var select = res.results[0].select_content;
                        checkedInput(select);
                    }
                }
            }
        })
    }

    //选中radio和checkBox
    function checkedInput(select) {
        if (select == undefined || select.length == 0){
            return;
        }

        select.split(",").forEach(function (item) {
            $('input[value="' + item + '"]').prop('checked', true);
        })
    }

    //初始化radio和checkBox不选中
    function uncheckInput() {
        $('input:checked').prop("checked", false);
    }

    //滚动到底部，加载下一张图片
    $('#img-container').on('scroll', function () {

        var sh = $(this)[0].scrollHeight;
        var h = $(this).height();
        var st = $(this)[0].scrollTop;

        if (sh - h - st <= 10) {

            if (!$('.loading').is(':hidden')) {
                return;
            }

            if (lastId == -1) {
                $('.loading').text('没有了...');
                $('.loading').show();
                return;
            }

            if ($('.loading').is(':hidden')) {
                $('.loading').show();
                fetchImages();
            }
        }
    });

</script>
